namespace QMVnSoft.Invoices
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCTBGF4Editor : FormControl
    {
        private BtnControl btnCancel;
        private BtnControl btnOk;
        private BtnControl btnShowAll;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column10;
        private DataGridViewTextBoxColumn Column11;
        private DataGridViewTextBoxColumn Column12;
        private NumericColumn Column13;
        private NumericColumn Column14;
        private NumericColumn Column15;
        private NumericColumn Column16;
        private DataGridViewCheckBoxColumn Column17;
        private DataGridViewTextBoxColumn Column18;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private NumericColumn Column5;
        private DataGridViewCheckBoxColumn Column6;
        private DataGridViewTextBoxColumn Column7;
        private DataGridViewTextBoxColumn Column8;
        private DataGridViewTextBoxColumn Column9;
        private IContainer components;
        private SplitContainer grdContainer;
        private GridControl grdDetail;
        private GridControl grdMaster;
        private bool ib_CheckAccepted;
        private bool ib_FirstTime = true;
        private object is_Ma_Dt;
        private object is_MasterKey;
        private object is_ParentDate;
        private object is_ParentKey;
        private HeaderCheck oDetailHeader;
        private AppInvoiceLib oAppInvoiceLib;
        private DataTable oLocalDetailTable;
        private DataTable oLocalMasterTable;
        private DataTable oLocalSelectedTable;
        private CTDHDataAccess oMethod = new CTDHDataAccess();

        public FormCTBGF4Editor(string ps_InvoiceType, object ps_ParentKey, object pd_ParentDate, object ps_Ma_Dt, DataTable po_SelectedTable)
        {
            this.InitializeComponent();
            this.InvoiceCode = (ps_InvoiceType == "1") ? "PBGM" : "PBGB";
            this.ParentKey = ps_ParentKey;
            this.ParentDate = pd_ParentDate;
            this.Ma_Dt = ps_Ma_Dt;
            this.LocalSelectedTable = po_SelectedTable;
            this.grdMaster.AutoGenerateColumns = false;
            this.grdDetail.AutoGenerateColumns = false;
            this.oDetailHeader = new HeaderCheck();
            this.Column17.HeaderCell = this.oDetailHeader;
            this.Column17.HeaderText = "";
            this.oDetailHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(this.oDetailHeader_OnCheckBoxClicked);
            this.OnResetControls();
            this.OnFillData();
            base.ResizeForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.OnSelected();
            this.CheckAccepted = true;
            base.Close();
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            this.ib_FirstTime = false;
            this.OnFillData();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataRow row = ((DataRowView) this.DetailGrid.CurrentRow.DataBoundItem).Row;
        }

        private void grdMaster_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataRow row = ((DataRowView) this.MasterGrid.CurrentRow.DataBoundItem).Row;
            this.SelectAllDetail((bool) row["Marked"]);
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            DataGridViewCellStyle style4 = new DataGridViewCellStyle();
            DataGridViewCellStyle style5 = new DataGridViewCellStyle();
            this.grdContainer = new SplitContainer();
            this.grdMaster = new GridControl();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.Column18 = new DataGridViewTextBoxColumn();
            this.Column5 = new NumericColumn();
            this.Column6 = new DataGridViewCheckBoxColumn();
            this.grdDetail = new GridControl();
            this.Column7 = new DataGridViewTextBoxColumn();
            this.Column8 = new DataGridViewTextBoxColumn();
            this.Column9 = new DataGridViewTextBoxColumn();
            this.Column10 = new DataGridViewTextBoxColumn();
            this.Column11 = new DataGridViewTextBoxColumn();
            this.Column12 = new DataGridViewTextBoxColumn();
            this.Column13 = new NumericColumn();
            this.Column14 = new NumericColumn();
            this.Column15 = new NumericColumn();
            this.Column16 = new NumericColumn();
            this.Column17 = new DataGridViewCheckBoxColumn();
            this.btnShowAll = new BtnControl();
            this.btnOk = new BtnControl();
            this.btnCancel = new BtnControl();
            this.grdContainer.Panel1.SuspendLayout();
            this.grdContainer.Panel2.SuspendLayout();
            this.grdContainer.SuspendLayout();
            ((ISupportInitialize) this.grdMaster).BeginInit();
            ((ISupportInitialize) this.grdDetail).BeginInit();
            base.SuspendLayout();
            this.grdContainer.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.grdContainer.Location = new Point(0, 0x2d);
            this.grdContainer.Name = "grdContainer";
            this.grdContainer.Orientation = Orientation.Horizontal;
            this.grdContainer.Panel1.Controls.Add(this.grdMaster);
            this.grdContainer.Panel2.Controls.Add(this.grdDetail);
            this.grdContainer.Size = new Size(0x31a, 0x1e7);
            this.grdContainer.SplitterDistance = 0xe3;
            this.grdContainer.SplitterWidth = 1;
            this.grdContainer.TabIndex = 0;
            this.grdMaster.AllowUserToAddRows = false;
            this.grdMaster.AllowUserToDeleteRows = false;
            this.grdMaster.CheckAutoSize = true;
            this.grdMaster.CheckColor = false;
            this.grdMaster.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdMaster.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column3, this.Column18, this.Column5, this.Column6 });
            this.grdMaster.Dock = DockStyle.Fill;
            this.grdMaster.Font = new Font("Arial", 8.25f);
            this.grdMaster.Location = new Point(0, 0);
            this.grdMaster.Name = "grdMaster";
            this.grdMaster.Size = new Size(0x31a, 0xe3);
            this.grdMaster.TabIndex = 0;
            this.grdMaster.CellEndEdit += new DataGridViewCellEventHandler(this.grdMaster_CellEndEdit);
            this.grdMaster.CellEnter += new DataGridViewCellEventHandler(this.MasterGrid_CellEnter);
            this.Column1.DataPropertyName = "Ngay_Ct";
            this.Column1.HeaderText = "Ngày";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Resizable = DataGridViewTriState.False;
            this.Column1.Width = 0x3e;
            this.Column2.DataPropertyName = "So_Ct";
            this.Column2.HeaderText = "Số";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 0x44;
            this.Column3.DataPropertyName = "Dien_Giai";
            this.Column3.HeaderText = "Diễn giải (VN)";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 0xfb;
            this.Column18.DataPropertyName = "Desc_EN";
            this.Column18.HeaderText = "Diễn giải (EN)";
            this.Column18.Name = "Column18";
            this.Column18.ReadOnly = true;
            this.Column18.Width = 250;
            this.Column5.DataPropertyName = "TTien_Nt";
            style.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = style;
            this.Column5.HeaderText = "Tổng tiền";
            this.Column5.Name = "Column5";
            this.Column5.ReadOnly = true;
            this.Column5.Resizable = DataGridViewTriState.True;
            this.Column5.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignAmount;
            this.Column6.DataPropertyName = "Marked";
            this.Column6.HeaderText = "";
            this.Column6.Name = "Column6";
            this.Column6.Resizable = DataGridViewTriState.False;
            this.Column6.Width = 20;
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.CheckAutoSize = true;
            this.grdDetail.CheckColor = false;
            this.grdDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdDetail.Columns.AddRange(new DataGridViewColumn[] { this.Column7, this.Column8, this.Column9, this.Column10, this.Column11, this.Column12, this.Column13, this.Column14, this.Column15, this.Column16, this.Column17 });
            this.grdDetail.Dock = DockStyle.Fill;
            this.grdDetail.Font = new Font("Arial", 8.25f);
            this.grdDetail.Location = new Point(0, 0);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new Size(0x31a, 0x103);
            this.grdDetail.TabIndex = 1;
            this.grdDetail.CellEndEdit += new DataGridViewCellEventHandler(this.grdDetail_CellEndEdit);
            this.Column7.DataPropertyName = "Ma_Vt";
            this.Column7.HeaderText = "Vật tư";
            this.Column7.Name = "Column7";
            this.Column7.ReadOnly = true;
            this.Column7.Width = 0x3d;
            this.Column8.DataPropertyName = "Ten_Vt";
            this.Column8.HeaderText = "Tên vật tư";
            this.Column8.Name = "Column8";
            this.Column8.ReadOnly = true;
            this.Column8.Width = 0x98;
            this.Column9.DataPropertyName = "Dvt";
            this.Column9.HeaderText = "Đvt";
            this.Column9.Name = "Column9";
            this.Column9.ReadOnly = true;
            this.Column9.Width = 30;
            this.Column10.DataPropertyName = "Ma_Bp";
            this.Column10.HeaderText = "Bộ phận";
            this.Column10.Name = "Column10";
            this.Column10.ReadOnly = true;
            this.Column10.Width = 0x3d;
            this.Column11.DataPropertyName = "Ma_Hd";
            this.Column11.HeaderText = "Hợp đồng";
            this.Column11.Name = "Column11";
            this.Column11.ReadOnly = true;
            this.Column11.Width = 0x3d;
            this.Column12.DataPropertyName = "Ghi_Chu";
            this.Column12.HeaderText = "Ghi ch\x00fa";
            this.Column12.Name = "Column12";
            this.Column12.ReadOnly = true;
            this.Column12.Width = 0x8a;
            this.Column13.DataPropertyName = "So_Luong9";
            style2.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column13.DefaultCellStyle = style2;
            this.Column13.HeaderText = "Số lượng";
            this.Column13.Name = "Column13";
            this.Column13.ReadOnly = true;
            this.Column13.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column13.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column13.Width = 0x39;
            this.Column14.DataPropertyName = "Gia_Nt9";
            style3.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column14.DefaultCellStyle = style3;
            this.Column14.HeaderText = "Đơn giá";
            this.Column14.Name = "Column14";
            this.Column14.ReadOnly = true;
            this.Column14.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column14.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignPrice;
            this.Column14.Width = 0x39;
            this.Column15.DataPropertyName = "Tien_Nt";
            style4.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column15.DefaultCellStyle = style4;
            this.Column15.HeaderText = "Thành tiền";
            this.Column15.Name = "Column15";
            this.Column15.ReadOnly = true;
            this.Column15.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column15.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignAmount;
            this.Column15.Width = 0x39;
            this.Column16.DataPropertyName = "Tien_Nt1";
            style5.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column16.DefaultCellStyle = style5;
            this.Column16.HeaderText = "Chi phí";
            this.Column16.Name = "Column16";
            this.Column16.ReadOnly = true;
            this.Column16.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column16.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignAmount;
            this.Column16.Width = 0x39;
            this.Column17.DataPropertyName = "Marked";
            this.Column17.FalseValue = "";
            this.Column17.HeaderText = "";
            this.Column17.Name = "Column17";
            this.Column17.ReadOnly = true;
            this.Column17.Resizable = DataGridViewTriState.False;
            this.Column17.TrueValue = "";
            this.Column17.Width = 20;
            this.btnShowAll.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnShowAll.Font = new Font("Arial", 8.25f);
            this.btnShowAll.Location = new Point(470, 12);
            this.btnShowAll.Name = "btnShowAll";
            this.btnShowAll.Size = new Size(100, 0x17);
            this.btnShowAll.TabIndex = 1;
            this.btnShowAll.Text = "Hiện tất";
            this.btnShowAll.UseVisualStyleBackColor = true;
            this.btnShowAll.Click += new EventHandler(this.btnShowAll_Click);
            this.btnOk.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0x240, 12);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(100, 0x17);
            this.btnOk.TabIndex = 1;
            this.btnOk.Text = "Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            this.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(0x2aa, 12);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(100, 0x17);
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "Huỷ bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x31a, 0x214);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.btnShowAll);
            base.Controls.Add(this.grdContainer);
            this.MinimumSize = new Size(0x322, 0x236);
            base.Name = "FormCTBGF4Editor";
            this.grdContainer.Panel1.ResumeLayout(false);
            this.grdContainer.Panel2.ResumeLayout(false);
            this.grdContainer.ResumeLayout(false);
            ((ISupportInitialize) this.grdMaster).EndInit();
            ((ISupportInitialize) this.grdDetail).EndInit();
            base.ResumeLayout(false);
        }

        private void MasterGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if ((this.MasterGrid.CurrentRow != null) || (this.MasterGrid.CurrentRow.DataBoundItem != null))
            {
                DataRow row = ((DataRowView) this.MasterGrid.CurrentRow.DataBoundItem).Row;
                this.MasterKey = row["Stt"].ToString();
            }
        }

        private void oDetailHeader_OnCheckBoxClicked(bool state)
        {
            this.SelectAllDetail(state);
        }

        private void OnFillData()
        {
            this.LocalMasterTable = this.oMethod.GetMasterQuotation(this.ib_FirstTime, AppInstance.Subsidiary, this.ParentKey, this.AppInvoiceParameters.InvoiceType, this.ParentDate, this.Ma_Dt);
        }

        private void OnResetControls()
        {
            if (!this.AppInvoiceParameters.BilingualUsed)
            {
                this.MasterGrid.Columns[3].Visible = false;
            }
            if (this.AppInvoiceParameters.AllowDepartmentControl != 2)
            {
                this.DetailGrid.Columns[3].Visible = false;
            }
            if (this.AppInvoiceParameters.AllowContractControl != 2)
            {
                this.DetailGrid.Columns[4].Visible = false;
            }
            if (!this.AppInvoiceParameters.AllowQuantityControl)
            {
                this.MasterGrid.Columns[4].Visible = false;
                this.DetailGrid.Columns[6].Visible = false;
                this.DetailGrid.Columns[8].Visible = false;
            }
            if (!this.AppInvoiceParameters.AllowExpensesControl)
            {
                this.DetailGrid.Columns[9].Visible = false;
            }
        }

        private void OnSelected()
        {
            if ((this.LocalDetailTable != null) && (this.LocalDetailTable.Rows.Count != 0))
            {
                foreach (DataRow row2 in this.LocalDetailTable.Rows)
                {
                    if (row2.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }
                    DataRow row = this.LocalSelectedTable.Rows.Find(row2["Ma_Vt"]);
                    if (((bool) row2["Marked"]) && (((row == null) || ((this.AppInvoiceParameters.InvoiceType == "1") && (((decimal) row["Gia"]) > ((decimal) row2["Gia"])))) || ((this.AppInvoiceParameters.InvoiceType == "2") && (((decimal) row["Gia"]) < ((decimal) row2["Gia"])))))
                    {
                        if (row == null)
                        {
                            row = this.LocalSelectedTable.NewRow();
                        }
                        row["Stt0_B"] = row2["Stt0"];
                        row["Ma_Bp"] = row2["Ma_Bp"];
                        row["Ten_Bp"] = row2["Ten_Bp"];
                        row["Ma_Hd"] = row2["Ma_Hd"];
                        row["Ten_Hd"] = row2["Ten_Hd"];
                        row["Ma_Vt"] = row2["Ma_Vt"];
                        row["Ten_Vt"] = row2["Ten_Vt"];
                        row["Dvt"] = row2["Dvt"];
                        row["Ghi_Chu"] = row2["Ghi_Chu"];
                        row["He_So9"] = row2["He_So9"];
                        row["So_Luong9"] = row2["So_Luong9"];
                        row["Gia_Nt9"] = row2["Gia_Nt9"];
                        row["Gia9"] = row2["Gia9"];
                        row["So_Luong"] = row2["So_Luong"];
                        row["Gia_Nt"] = row2["Gia_Nt"];
                        row["Gia"] = row2["Gia"];
                        row["Tien_Nt"] = row2["Tien_Nt"];
                        row["Tien"] = row2["Tien"];
                        row["Tien_Nt1"] = row2["Tien_Nt1"];
                        row["Tien1"] = row2["Tien1"];
                        if (row.RowState == DataRowState.Detached)
                        {
                            this.LocalSelectedTable.Rows.Add(row);
                        }
                    }
                }
            }
        }

        private void OnShowDetail()
        {
            this.OnSelected();
            this.LocalDetailTable = this.oMethod.GetDetailQuotation(this.MasterKey);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Return))
            {
                this.OnSelected();
                this.CheckAccepted = true;
                base.Close();
            }
            else if (keyData == Keys.Escape)
            {
                base.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void SelectAllDetail(bool pb_State)
        {
            if ((this.LocalDetailTable != null) && (this.LocalDetailTable.Rows.Count != 0))
            {
                foreach (DataRow row in this.LocalDetailTable.Rows)
                {
                    if (row.RowState != DataRowState.Deleted)
                    {
                        this.SelectOneDetail(row, pb_State);
                    }
                }
            }
        }

        private void SelectOneDetail(DataRow po_Row, bool pb_State)
        {
            if (po_Row != null)
            {
                if (pb_State)
                {
                    po_Row["Marked"] = true;
                }
                else
                {
                    po_Row["Marked"] = false;
                }
            }
        }

        public bool CheckAccepted
        {
            get
            {
                return this.ib_CheckAccepted;
            }
            set
            {
                this.ib_CheckAccepted = value;
            }
        }

        private GridControl DetailGrid
        {
            get
            {
                return this.grdDetail;
            }
        }

        private AppInvoiceLib AppInvoiceParameters
        {
            get
            {
                if (this.oAppInvoiceLib == null)
                {
                    this.AppInvoiceParameters = new AppInvoiceLib();
                }
                return this.oAppInvoiceLib;
            }
            set
            {
                this.oAppInvoiceLib = value;
            }
        }

        private string InvoiceCode
        {
            get
            {
                if (this.AppInvoiceParameters == null)
                {
                    return "";
                }
                return this.AppInvoiceParameters.InvoiceCode;
            }
            set
            {
                if (this.AppInvoiceParameters != null)
                {
                    this.AppInvoiceParameters.InvoiceCode = value;
                    if (!string.IsNullOrEmpty(this.AppInvoiceParameters.InvoiceCode))
                    {
                        this.Text = this.AppInvoiceParameters.InvoiceName;
                    }
                }
            }
        }

        private DataTable LocalDetailTable
        {
            get
            {
                return this.oLocalDetailTable;
            }
            set
            {
                this.oLocalDetailTable = value;
                this.DetailGrid.DataSource = new BindingSource(this.oLocalDetailTable, this.oLocalDetailTable.TableName);
            }
        }

        private DataTable LocalMasterTable
        {
            get
            {
                return this.oLocalMasterTable;
            }
            set
            {
                this.oLocalMasterTable = value;
                this.MasterGrid.DataSource = new BindingSource(this.oLocalMasterTable, this.oLocalMasterTable.TableName);
            }
        }

        private DataTable LocalSelectedTable
        {
            get
            {
                return this.oLocalSelectedTable;
            }
            set
            {
                this.oLocalSelectedTable = value;
            }
        }

        private object Ma_Dt
        {
            get
            {
                return this.is_Ma_Dt;
            }
            set
            {
                this.is_Ma_Dt = value;
            }
        }

        private GridControl MasterGrid
        {
            get
            {
                return this.grdMaster;
            }
        }

        private object MasterKey
        {
            get
            {
                return this.is_MasterKey;
            }
            set
            {
                if (this.is_MasterKey != value)
                {
                    this.is_MasterKey = value;
                    this.OnShowDetail();
                }
            }
        }

        private object ParentDate
        {
            get
            {
                return this.is_ParentDate;
            }
            set
            {
                this.is_ParentDate = value;
            }
        }

        private object ParentKey
        {
            get
            {
                return this.is_ParentKey;
            }
            set
            {
                this.is_ParentKey = value;
            }
        }
    }
}

